//
//  WBVisitorView.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/5.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit
import SnapKit

protocol WBVisitorViewDelegate:NSObjectProtocol {
    
    func loginOperation()
}

class WBVisitorView: UIView {
    
    weak var delegate:WBVisitorViewDelegate?
    /// 旋转视图
    fileprivate lazy var cycleImageView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    /// 罩层图片
    fileprivate lazy var makeIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 主页图片
    fileprivate lazy var homeIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 消息文本
    fileprivate lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .center
        
        return label
    }()
    /// 注册
    fileprivate lazy var registerButton: UIButton = {
        
        let button = UIButton()
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        let image = UIImage(named: "common_button_white_disable")!
        let stretImage = image.resizableImage(withCapInsets: UIEdgeInsets(top:5,left:5,bottom:5,right:5), resizingMode: .stretch)
        button.setBackgroundImage(stretImage, for: .normal)
        // 去掉点击高亮
        button.adjustsImageWhenDisabled = false
        button.addTarget(self, action: #selector(clickRegisterButton), for: .touchUpInside)
        
        return button
    }()
    
    fileprivate lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        // 根据四边拉伸图片
        let image = UIImage(named: "common_button_white_disable")!
        let stretImage = image.resizableImage(withCapInsets: UIEdgeInsets(top:5,left:5,bottom:5,right:5), resizingMode: .stretch)
        button.setBackgroundImage(stretImage, for: .normal)
        // 去掉点击高亮
        button.adjustsImageWhenDisabled = false
        button.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        
        return button
    }()

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
                
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        /// 添加旋转视图
        addSubview(cycleImageView)
        cycleImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        /// 添加罩层图片
        addSubview(makeIconImageView)
        makeIconImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
        }
        /// 添加主页视图
        addSubview(homeIconImageView)
        homeIconImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
        }
        /// 添加文本
        addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(cycleImageView.snp.bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        // 添加注册按钮
        addSubview(registerButton)
        registerButton.snp.makeConstraints { (make) in
            
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(8)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        //添加登录按钮
        addSubview(loginButton)
        loginButton.snp.makeConstraints { (make) in
            
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
    }
    
    //登录按钮
    @objc private func clickLoginButton() {
        // 执行代理
        delegate?.loginOperation()
        
    }
    //注册
    @objc private func clickRegisterButton() {
        // 执行代理
        delegate?.loginOperation()
    }
    
    // 旋转视图动画
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //目的地
        animation.toValue = 2 * Double.pi
        //时长
        animation.duration = 20
        // 次数, 设置最大值
        animation.repeatCount = MAXFLOAT
        // 切换页面的时候不然其删除动画
        animation.isRemovedOnCompletion = false
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
    func upDateVisitorViewInfo(imageName:String?,message:String?) {
        
        if let img = imageName, let mes = message {
            // 不是首页
            homeIconImageView.image = UIImage(named: img)
            messageLabel.text = mes
            // 隐藏旋转视图
            cycleImageView.isHidden = true
            
        } else {
            // 是首页, 就开启动画
            startAnimation()
        }
        
    }
}
